package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.cart.client.CartFeignClient;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @Description: TODO
 * @Author: Mr.Cao
 * @DateTime: 2021/10/11 21:03
 * BMW F750GS
 **/
@Controller
public class CartInfoController {

    @Autowired
    private CartFeignClient cartFeignClient;


    //加入购物车 --只进行加入购物车的操作
    @GetMapping("/addCart.html")
    public String addCart(Long skuId, Integer skuNum, RedirectAttributes redirectAttributes){
         cartFeignClient.addToCart(skuId, skuNum);
        //设置重定向的参数
        redirectAttributes.addAttribute("sId",skuId);
        redirectAttributes.addAttribute("sN",skuNum);
        //重定向到加入成功,回显页面
        return "redirect:http://item.gmall.com/toCart.html";
    }
    //回显加入成功页面
    @GetMapping("toCart.html")
    public String toCart(Long sId, Integer sN, Model model){
        CartInfo cartInfo =  cartFeignClient.toCart(sId);
        cartInfo.setSkuNum(sN);//设置本次加入购物车的数量
        model.addAttribute("cartInfo",cartInfo);
        return "cart/addCart";
    }
    //去购物车结算 跳转到购物车页面
    @GetMapping("/cart.html")
    public String cartList(){

        return "cart/index";
    }
}
